﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace Xcelligent.HyperR
{
    class RecoveryUtility
    {

        #region Objects

        private Properties.Settings Settings = new Properties.Settings();
        private System.Diagnostics.Process Process;

        #endregion


        #region Properties

        private string Snapshots
        { get { return string.Format("{0}{1}", Settings.CustomVMDirectory, Settings.SnapshotDirectory); } }
        private string VirtualMachines
        { get { return string.Format("{0}{1}", Settings.CustomVMDirectory, Settings.VirtualMachinesDirectory); } }
        private string _VMPath = string.Empty;
        public string VMPath
        {
            get { return _VMPath; }
            set { _VMPath = value; }
        }
        // Process criteria to test if the VMPath is a valid Virtual Machine path.
        public bool IsValid
        { get { throw new NotImplementedException("VMRecover.IsValid"); } }

        #endregion


        #region Init

        public RecoveryUtility(string VMPath)
        {
            this.VMPath = VMPath;
            //XElement Files = GetFileSystem(VMPath);
        }

        #endregion


        #region Main Methods

        #endregion


        #region Helper Methods

        static XName Root = "Root";
        static XName Snapshot = "Snapshot";
        static XName VirtualMachine = "VirtualMachine";

        private static XElement GetFiles()
        {
            XElement XmlFiles = new XElement(Root);
            // TODO:
            // Build XElement to store info from the xml files
            // in the Snapshot and Virtual Machine directories.
            return XmlFiles;
        }

        // Members and method for testing.
        static XName Dir = "Dir";
        static XName Name = "Name";
        static XName File = "File";
        static XName Length = "Length";
        private static XElement GetFileSystem(string source)
        {
            DirectoryInfo di = new DirectoryInfo(source);
            return new XElement(Dir,
                new XAttribute(Name, di.Name),
                from d in Directory.GetDirectories(source)
                select GetFileSystem(d),
                from fi in di.GetFiles()
                select new XElement(File,
                    new XElement(Name, fi.Name),
                    new XElement(Length, fi.Length)
                )
            );
        }

        private System.Diagnostics.ProcessStartInfo SnapshotStartInfo(string Path, string Filename)
        { return StartInfo(Path, Snapshots, Filename); }

        private System.Diagnostics.ProcessStartInfo VirtualMachineStartInfo(string Path, string Filename)
        { return StartInfo(Path, VirtualMachines, Filename); }

        private System.Diagnostics.ProcessStartInfo StartInfo(string MachinePath, string SystemPath, string Filename)
        {
            string LinkFullname = string.Format("\"{0}{1}\"", SystemPath, Filename);
            string Fullname = string.Format("\"{0}{1}\"", MachinePath, Filename);
            System.Diagnostics.ProcessStartInfo SI = new System.Diagnostics.ProcessStartInfo();
            SI.WorkingDirectory = MachinePath;
            SI.Arguments = string.Format("{0} {1}", LinkFullname, Fullname);
            SI.FileName = "mklink.exe";
            SI.UseShellExecute = false;
            SI.RedirectStandardOutput = false;
            SI.RedirectStandardError = true;
            return SI;
        }

        private void MKLink(System.Diagnostics.ProcessStartInfo StartInfo)
        {
            Process = new System.Diagnostics.Process();
            Process.StartInfo = StartInfo;
            Process.Start();
            Process.WaitForExit();
            Process.Close();
        }

        #endregion

    }
}
